#/usr/bin/python
# -*- coding: utf-8 -*-
'''

Created on 9 nov. 2012

@author: Ludovic Barrière and Steven Marguet, Institut Clément Ader, 135 av. de Rangueil, 31077 Toulouse, France

© copyright:
    
    TODO

'''

from mpi4py import MPI
import scipy.sparse as sp
import scipy.linalg as la
import numpy as np
import stepManager
from domain.domain import Domain, GlobalDomain
from ioparser.parserModel import parserPatran

class Problem():
    ''' This class defines an object representing the problem to be solved. 
    
        A Problem gathers data concerning a model and parameters. Parameters may be related to 
        the model itself or to the simulation and solution procedure.
        
        A Problem starts and stops the solution procedure. It can be seen as the main interface object to
        users. It is the customer of others 
        
        attributes:
        
        - self.domain is the model of the structure (instance of "domain.Domain()")
        - self.stepManager is the tool to control loadings and boundary conditions applied 
        on the structure. (instance of "stepManager.StepManager()")
        - self.solver is the current solver (instance of "solver.Solver()")
        
        methods:
        
        - start() method is dedicated to start a simulation 
        - stop() method is dedicated to stop a simulation before completed
        
    '''
    
    def __init__(self, inputfile, paramfile=None, COMM=None, log=None):
        ''' Constructor of a Problem instance '''
        
        self.domain = None
        self.stepManager = None
        self.parser = None
        self.comm = COMM
        self.log = log
        self.inputfile = inputfile
        self.paramfile = paramfile
        self.initialize(inputfile)
        
    def initialize(self, inputfile, **kwargs):
        '''
            @param inputfile: str, optional
            @param parser:   
        '''
        
        self.parser = parserPatran.Parser(inputfile)
        
        self.parser.read()
        
        self.domain, self.stepManager = self.parser.buildModel()
        
        return 0
           
    def start(self):
        ''' Runs the simulation.
        '''
        # Loop on steps:
        while self.stepManager.setNextStep(self.domain):
            # Analysis of the step
#            analysis = step.analysis()
#            # 
#            analysis.setDomain(domain)
#            analysis.setLoadCases(step.loadcases)
#            analysis.setIncrementManager(step.incrementManager)
#            analysis.setSolvingStrategy(step.solvingStrategy)
#            
#            self.domain = analysis.start()
#            
            self.domain = self.stepManager.startStepAnalysis(self.domain)
            self.domain.saveResults()
            
                
    def stop(self):
        ''' Stops the running simulation. 
        
            When called, this method tries to save results that have
            been obtained before. 
            
        '''
        pass
    
    def setDomain(self,domain):
        
        self.domain = domain
    
    def setStepManager(self,stepmanager):
    
        self.stepManager = stepmanager
        
    
    
    
        
     